﻿/* Copyright 2010 Igor Francé
 * 
 * This file is part of ECMADoc (http://ecmadoc.codeplex.com).
 * 
 * ECMADoc is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * ECMADoc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with ECMADoc. 
 * If not, see http://www.gnu.org/licenses/.
 */

namespace EcmaDoc
{
	using System;
	using System.IO;
	using System.Reflection;

	using EcmaDoc.Core;

	internal class Program
	{
		#region Constants and Fields

		private static string lastOperation;

		#endregion

		#region Properties

		private static string ProgramName
		{
			get
			{
				return Assembly.GetExecutingAssembly().GetName().Name.ToLower();
			}
		}

		#endregion

		#region Methods

		private static void Exit(int code)
		{
			Environment.ExitCode = code;
		}

		private static void Main(string[] args)
		{
			if (args.Length == 0 || string.IsNullOrEmpty(args[0]))
			{
				ShowHelp();
				return;
			}

			string projectPath = args[0];
			if (!File.Exists(projectPath))
			{
				ShowError("The specified project file '{0}' doesn't exist", projectPath);
				Exit(1);
			}

			try
			{
				Project project = Project.Create(projectPath);
				project.ProcessProgress += OnProjectProgress;
				project.ExecuteOperation("Process project");

				Console.WriteLine("Done");
				Exit(0);
			}
			catch (Exception e)
			{
				Exception source = e;
				while (source.InnerException != null)
				{
					source = source.InnerException;
				}

				Log.WriteLine(LogCategory.Error, source.Message);
				Log.WriteLine(LogCategory.Error, source.StackTrace);

				Console.WriteLine(source.Message);
				Exit(1);
			}
		}

		private static void OnProjectProgress(object sender, ProjectOperationProgressEventArgs e)
		{
			if (lastOperation != e.CurrentStepName)
			{
				Console.WriteLine("Step {0} of {1}: {2}", e.StepNumber, e.StepCount, e.CurrentStepName);
			}

			lastOperation = e.CurrentStepName;
		}

		private static void ShowDescription()
		{
			Console.WriteLine("Generates API documentation as HTML document out of JavaScript source files.");
			Console.WriteLine();
		}

		private static void ShowError(string message, params object[] args)
		{
			Console.WriteLine(message, args);
		}

		private static void ShowHelp()
		{
			ShowDescription();
			ShowUsage();
		}

		private static void ShowUsage()
		{
			Console.WriteLine("{0} project_file", ProgramName);
			Console.WriteLine();
			Console.WriteLine("  project_file     The path to the project definition file.");
		}

		#endregion
	}
}